package com.jy.project.service.imp;
import com.jy.project.domain.Apply;
import com.jy.project.domain.Material;
import com.jy.project.domain.User;
import com.jy.project.dto.*;
import com.jy.project.enums.ApplyTypeEnum;
import com.jy.project.ex.BizException;
import com.jy.project.mapper.*;
import com.jy.project.service.ApplyService;
import com.jy.project.service.imp.defult.DefaultApplyBusinessImp;
import com.jy.project.util.JwtUtils;
import com.jy.project.util.ServiceResultUtils;
import com.jy.project.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import tk.mybatis.mapper.entity.Example;
import java.util.List;


@Service("applyInsertMaterialServiceImpl")
@Slf4j
public class ApplyInsertMaterialServiceImpl extends DefaultApplyBusinessImp implements ApplyService {


    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private ApplyCollectMapper applyCollectMapper;

    @Autowired
    private ApplyBorrowMapper applyBorrowMapper;

    @Autowired
    private ApplyReturnMapper applyReturnMapper;

    @Autowired
    private ApplyRenewMapper  applyRenewMapper;

    @Autowired
    private ApplyStockReturnMapper  applyStockReturnMapper;


    @Autowired
    DataSourceTransactionManager dataSourceTransactionManager;


    @Autowired
    TransactionDefinition transactionDefinition;

    public boolean insertApply(ApplyDTO dto) {

        return true;
    }

    @Override
    public List<ApplyQualitVO> listApplyQualityVO(ApplyQueryDTO dto) {

        return null;
    }

    @Override
    public List<ApplyDeptAdminVO> listDeptAdminApplyVO(ApplyDeptAdminQueryDTO dto) {
        return null;
    }

    @Override
    public boolean applyAdopt(ApplyAdoptDTO dto) {
        log.info("审批通过入库单");
        if (StringUtils.isEmpty(dto.getApplyNo())){
            throw  new BizException("审批单号不能为空");
        }
        Apply apply = applyMapper.getApplyByApplyNo(dto.getApplyNo());
        if (apply==null){
            throw  new BizException("没有此审批记录");
        }
        Example example =new Example(Material.class);
        example.createCriteria()
                .andEqualTo("materialNo",apply.getMaterialNo());
        Material materials = materialMapper.selectOneByExample(example);
        if (materials.getStatus()==0){
            return true;
        }
        Material material = new Material();
        material.setStatus(0);
        return materialMapper.updateByExampleSelective(material,example)>0;

    }

    @Override
    public boolean applyReject(ApplyAdoptDTO dto) {
        log.info("审批通过入库单");
        if (StringUtils.isEmpty(dto.getApplyNo())){
            throw  new BizException("审批单号不能为空");
        }
        Apply apply = applyMapper.getApplyByApplyNo(dto.getApplyNo());
        if (apply==null){
            throw  new BizException("apply驳回失败");
        }
        Example example =new Example(Material.class);
        example.createCriteria()
                .andEqualTo("materialNo",apply.getMaterialNo());
        Material materials = materialMapper.selectOneByExample(example);
        if (materials.getStatus()==2){
            return true;
        }
        Material material = new Material();
        material.setStatus(2);
        return materialMapper.updateByExampleSelective(material,example)>0;
    }

    @Override
    public ServiceResultUtils listMaterialDetailsVO(MaterialDetailsDTO dto) {

        return null;
    }

    @Override
    public List<ApplyUserVO> listUserApplyVO(ApplyUserQueryDTO dto) {

        dto.setApplyType(ApplyTypeEnum.INSERT_MATERIAL.getCode());
        return applyMapper.listUserApplyVO(dto);
    }

    @Override
    public boolean insertApplyBusiness(ApplyDTO dto) {

        return  true;
    }

    private  List<MaterialVO> checkMaterialNos(String[] split ){

        return  null;
    }
}
